package hedron.dialog;

import static hedron.i18n.Internationalization._;
import hedron.Application;
import hedron.ui.UIFactory;
import java.util.Map;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class LoginDialog extends AbstractDialog {
	static String lastLoggedUserName = "";

	Text userNameText;
	Text passwordText;

	private int attempts;

	public LoginDialog(Shell parentShell) {
		super(parentShell);
		attempts = 3;
	}

	@Override
	public void create() {
		super.create();
		setTitle(_("Application.Name"));
		setMessage(_("Dialog.LogIn.Message"), 0);
	}

	@Override
	protected void okPressed() {
		// TODO: check user and pass
		lastLoggedUserName = userNameText.getText();
		if (Application.authenticateUserPass(lastLoggedUserName, passwordText.getText())) {
			setReturnCode(OK);
			close();
		} else {
			if (--attempts <= 0) {
				setReturnCode(CANCEL);
				close();
				return;
			}
			passwordText.setText("");
			setMessage(_("Dialog.LogIn.Error"), 2);
		}
	}

	private static final String layout = "<layout columns=\"2\" width=\"400\" align=\"FILL\" valign=\"FILL\">"
			+ "<label text=\"Dialog.LogIn.UserName\" width=\"100\" align=\"BEGINING\" valign=\"CENTER\"/>"
			+ "<text id=\"userNameText\" align=\"FILL\" valign=\"CENTER\"/>"
			+ "<label text=\"Dialog.LogIn.Password\" width=\"100\" align=\"BEGINING\" valign=\"CENTER\"/>"
			+ "<text id=\"passwordText\" align=\"FILL\" valign=\"CENTER\"/>"
			+ "</layout>";

	@Override
	void createContent(Composite parent) {
		Map<String, Control> components = UIFactory.createUI(parent, layout, null);
		userNameText = (Text) components.get("userNameText");
		userNameText.setText(lastLoggedUserName);
		passwordText = (Text) components.get("passwordText");
		passwordText.setEchoChar('*');
	}

	private static final String layoutFooter = "<layout columns=\"3\" align=\"FILL\" valign=\"END\">"
			+ "<label align=\"FILL\" valign=\"CENTER\"/>"
			+ "<button id=\"exit\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"END\"/>"
			+ "<button id=\"ok\" default=\"true\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"END\"/>"
			+ "</layout>";

	@Override
	void createFooter(Composite parent) {
		Composite grid = new Composite(parent, 0);
		Map<String, Control> components = UIFactory.createUI(grid, layoutFooter, null);

		Button cancel = (Button) components.get("exit");
		cancel.setText("Exit");
		cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				setReturnCode(CANCEL);
				close();
			}
		});

		Button ok = (Button) components.get("ok");
		ok.setText("OK");
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				okPressed();
			}
		});
	}
}
